# -*-makefile-*-
# @configure_input@
# Makefile.global.in - Makefile to be included by all submakes
#
# This file is converted by configure into an actual Makefile,
# replacing the @varname@ placeholders by actual values.
#
# This files is intended to contain infrastructure needed by several
# makefiles, particulary central handling of compilation flags and
# rules.

citus_abs_srcdir:=@abs_top_srcdir@/${citus_subdir}
citus_abs_top_srcdir:=@abs_top_srcdir@
postgres_abs_srcdir:=@POSTGRES_SRCDIR@
postgres_abs_builddir:=@POSTGRES_BUILDDIR@

PG_CONFIG:=@PG_CONFIG@
PGXS:=$(shell $(PG_CONFIG) --pgxs)

# if both, git is installed and there is a .git directory in the working dir we set the
# GIT_VERSION to a human readable gitref that resembles the version from which citus is
# built. During releases it will show the tagname which by convention is the verion of the
# release
ifneq (@GIT_BIN@,)
ifneq (@HAS_DOTGIT@,)
	# try to find a tag that exactly matches the current branch, swallow the error if cannot find such a tag
	GIT_VERSION := "$(shell @GIT_BIN@ describe --exact-match --dirty --always --tags 2>/dev/null)"

	# if there is not a tag that exactly matches the branch, then GIT_VERSION would still be empty
	# in that case, set GIT_VERSION with current branch's name and the short sha of the HEAD
ifeq ($(GIT_VERSION),"")
	GIT_VERSION := "$(shell @GIT_BIN@ rev-parse --abbrev-ref HEAD)(sha: $(shell @GIT_BIN@ rev-parse --short HEAD))"
endif
endif
endif

# Support for VPATH builds (i.e. builds from outside the source tree)
vpath_build=@vpath_build@
ifeq ($(vpath_build),yes)
	override VPATH:=$(citus_abs_srcdir)
	USE_VPATH:=$(VPATH)
	citus_top_srcdir:=$(citus_abs_top_srcdir)
	override srcdir=$(VPATH)
else
	citus_top_srcdir:=$(citus_top_builddir)
endif

# Citus is built using PostgreSQL's pgxs
USE_PGXS=1
include $(PGXS)

# Remake Makefile.global from Makefile.global.in if the latter
# changed. In order to trigger this rule, the including file must
# write `include $(citus_top_builddir)/Makefile.global', not some
# shortcut thereof.  This makes it less likely to accidentally run
# with some outdated Makefile.global.
# Make internally restarts whenever included Makefiles are
# regenerated.
$(citus_top_builddir)/Makefile.global: $(citus_abs_top_srcdir)/configure $(citus_top_builddir)/Makefile.global.in $(citus_top_builddir)/config.status
	cd @abs_top_builddir@ && ./config.status Makefile.global

# Ensure configuration is generated by the most recent configure,
# useful for longer existing build directories.
$(citus_top_builddir)/config.status: $(citus_abs_top_srcdir)/configure $(citus_abs_top_srcdir)/src/backend/columnar/columnar.control
	cd @abs_top_builddir@ && ./config.status --recheck && ./config.status

# Regenerate configure if configure.in changed
$(citus_abs_top_srcdir)/configure: $(citus_abs_top_srcdir)/configure.in
	cd ${citus_abs_top_srcdir} && ./autogen.sh

# If specified via configure, replace the default compiler. Normally
# we'll build with the one postgres was built with. But it's useful to
# be able to use a different one, especially when building against
# distribution packages.
ifneq (@CC@,)
    override CC=@CC@
endif

# If detected by our configure script, override the FLEX postgres
# detected.  That allows to compile citus against a postgres which was
# built without flex available (possible because generated files are
# included)
ifneq (@FLEX@,)
    override FLEX=@FLEX@
endif

# Add options passed to configure or computed therein, to CFLAGS/CPPFLAGS/...
override CFLAGS += @CFLAGS@ @CITUS_CFLAGS@
override BITCODE_CFLAGS := $(BITCODE_CFLAGS) @CITUS_BITCODE_CFLAGS@
ifneq ($(GIT_VERSION),)
    override CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
endif
override CPPFLAGS := @CPPFLAGS@ @CITUS_CPPFLAGS@ -I '${citus_abs_top_srcdir}/src/include' -I'${citus_top_builddir}/src/include' $(CPPFLAGS)
override LDFLAGS += @LDFLAGS@ @CITUS_LDFLAGS@

# optional file with user defined, additional, rules
-include ${citus_abs_srcdir}/src/Makefile.custom
